package com.local.runnable.newSingleThreadExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.joda.time.DateTime;
import org.joda.time.Period;
/**
 * newSingleThreadExecutor
 *创建一个单线程的线程池。
 *这个线程池只有一个线程在工作，也就是相当于单线程串行执行所有任务。
 *如果这个唯一的线程因为异常结束，那么会有一个新的线程来替代它。
 *此线程池保证所有任务的执行顺序按照任务的提交顺序执行。
 * @author Administrator
 *
 */
public class NewSingleThread {
	public static void main(String[] args) {
	
		//方法1：
//		ExecutorService pool = Executors.newSingleThreadExecutor();
//		//创建实现了Runnable接口对象，Thread对象当然也实现了Runnable接口
//        Thread t1 = new MyThread("线程1");
//        Thread t2 = new MyThread("线程2");
//        Thread t3 = new MyThread("线程3");
//        Thread t4 = new MyThread("线程4");
//        Thread t5 = new MyThread("线程5");
//        Thread t6 = new MyThread("线程6");
//        Thread t7 = new MyThread("线程7");
//        Thread t8 = new MyThread("线程8");
//        Thread t9 = new MyThread("线程9");
//        Thread t10 = new MyThread("线程10");
//        Thread t11 = new MyThread("线程11");
//        Thread t12 = new MyThread("线程12");
//        Thread t13 = new MyThread("线程13");
//        Thread t14 = new MyThread("线程14");
//        Thread t15 = new MyThread("线程15");
//        Thread t16 = new MyThread("线程16");
//        Thread t17 = new MyThread("线程17");
//        Thread t18 = new MyThread("线程18");
//        Thread t19 = new MyThread("线程19");
//        Thread t20 = new MyThread("线程20");
//      //将线程放入池中进行执行
//        pool.execute(t1);
//        pool.execute(t2);
//        pool.execute(t3);
//        pool.execute(t4);
//        pool.execute(t5);
//        pool.execute(t6);
//        pool.execute(t7);
//        pool.execute(t8);
//        pool.execute(t9);
//        pool.execute(t10);
//        pool.execute(t11);
//        pool.execute(t12);
//        pool.execute(t13);
//        pool.execute(t14);
//        pool.execute(t15);
//        pool.execute(t16);
//        pool.execute(t17);
//        pool.execute(t18);
//        pool.execute(t19);
//        pool.execute(t20);
//        //关闭线程池
//        pool.shutdown();
		
		//方法2（单线程串行执行所有任务）：
		DateTime dateTime=new DateTime();
		System.out.println("开始执行:"+dateTime.toString("yyyy-MM-dd HH:mm:ss SSS"));
		ExecutorService pool = Executors.newSingleThreadExecutor();
		pool.execute(new Runnable(){
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				DateTime dateTime2=new DateTime();
				System.out.println("线程A执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
			}
		});
		
		pool.execute(new Runnable(){
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				DateTime dateTime2=new DateTime();
				System.out.println("线程B执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
			}
		});
		
		pool.execute(new Runnable(){
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				DateTime dateTime2=new DateTime();
				System.out.println("线程C执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
			}
		});
				
		
		pool.execute(new Runnable(){
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				DateTime dateTime2=new DateTime();
				System.out.println("线程Z执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
			}
		});
		
		pool.execute(new Runnable(){
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				DateTime dateTime2=new DateTime();
				System.out.println("线程D执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
			}
		});
				
		pool.execute(new Runnable(){
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				DateTime dateTime2=new DateTime();
				System.out.println("线程E执行："+dateTime2.toString("yyyy-MM-dd HH:mm:ss SSS"));
			}
		});
		pool.shutdown();
		while (true) {  //判断所有线程是否执行完
			if (pool.isTerminated()) {  
				DateTime dateTime2=new DateTime();
				 Period period=new Period(dateTime,dateTime2);
				 Integer hours = period.getHours();
				 Integer minutes = period.getMinutes();
				 Integer seconds = period.getSeconds();
				System.out.println("所有的任务已执行完毕！花费时间为："+hours+"时"+minutes+"分"+seconds+"秒");  
				break;  
			}  
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
		} 
		
	}
}
